package logic.ui.componentInfoMed
{
	import common.manager.GUI;
	import common.manager.ObserverEvt;
	import common.manager.Resource;
	
	import component.show.containers.Image;
	import component.show.containers.SimpleBtn;
	import component.show.control.UISprite;
	import component.untils.ComponentItem;
	
	import fl.controls.ComboBox;
	import fl.controls.TextInput;
	import fl.data.DataProvider;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import logic.def.Def;
	import logic.def.UIURL;
	import logic.manager.XMLManager;
	
	public class SimpleBtnInfoMed extends UISprite
	{
		public static const NAME : String = "SimpleBtn";
		public static const SELECTIMAGE : String = "ImageInfoMed_SelectImage";
		
		private var m_target : SimpleBtn;	
		private var m_tarComItem : ComponentItem;
		
		private var m_txtID : TextInput;
		
		private var m_comSkinLst : ComboBox;
		
		private var m_dpSkinLst : DataProvider;
		
		private var m_registFunGroup : Object;
		
		public function SimpleBtnInfoMed()
		{
			super(NAME,UIURL["SimpleBtn"]);
			
			m_registFunGroup = new Object;
			
			init();
		}
		
		public override function destory() : void
		{
			m_txtID.removeEventListener(Event.CHANGE,onIdChanged);
			m_comSkinLst.removeEventListener(Event.CHANGE,onSelectImage);
			
			m_txtID = null;
			m_comSkinLst = null;
		}
		
		public  function init() : void
		{
			m_txtID = collection["txtId"];
			m_comSkinLst = collection["Com_SkinLst"];
			
			m_dpSkinLst = new DataProvider;
			m_comSkinLst.dataProvider = m_dpSkinLst;
			
			m_comSkinLst.labelField = "id";
			
			var skinGroup : Object = XMLManager.SkinRef.simpleBtn;
			var skinLst : Array = [];
			
			for each(var item : Object in skinGroup)
			{
				skinLst.push(item);
			}
			
			skinLst.sortOn("sort",Array.NUMERIC);
			
			m_dpSkinLst.addItems(skinLst);
			
			m_comSkinLst.addEventListener(Event.CHANGE,onSelectImage);
			m_txtID.addEventListener(Event.CHANGE,onIdChanged);
		}
	
		public function set target(image : ComponentItem) : void
		{
			m_target = image.item;
			m_tarComItem = image;
	
			var skinName : String;
			
			if(m_target.skinName)
			{
				setComSkinSelectedItem();
			}
			else
			{
				skinName = m_dpSkinLst.toArray()[0]["skin"];
				
				m_target.skinName = skinName;
			}
			
			setTargetData( );
			
			ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
		}
		
		/**
		 * 设置数据 
		 */		
		public function setTargetData() : void
		{
			m_txtID.text = m_target.name;
		}
		
		private function onIdChanged(evt : Event) : void
		{
			m_target.name = m_txtID.text;
		}
		
		private function onSelectImage(evt : Event) : void
		{
			var cellData : Object = m_comSkinLst.selectedItem;
			
			m_target.skinName = cellData["skin"];
		}
		
		private function drawImage() : void
		{
			var ind : int = m_comSkinLst.selectedIndex;
			
			if(ind < 0)
			{
				ind = 0;
			}
			
			var cellData : Object = m_comSkinLst.getItemAt(ind);
			
			ObserverEvt.Send(SELECTIMAGE,cellData);
		}
		
		/**
		 * 设置选择的skin 
		 */		
		private function setComSkinSelectedItem() : void
		{	
			var skinNameLst : Array = m_dpSkinLst.toArray();
			var length : int = skinNameLst.length;
			
			for(var i:int=0;i<length;i++)
			{
				var str : String = skinNameLst[i]["skin"];
				
				if(m_target.skinName == str)
				{
					m_comSkinLst.selectedItem = skinNameLst[i];
					break;
				}
			}
		}
	}
}